(function ($) {
    $.extend({
        // 表格封装处理
        table: {},
        // 表单封装处理
        form: {},
        // 弹出层封装处理
        modal: {
            // 显示图标
            icon: function(type) {
                var icon = "";
                if(type == modal_status.WARNING) {
                    icon = 0;
                } else if(type == modal_status.SUCCESS) {
                    icon = 1;
                } else if(type == modal_status.FAIL) {
                    icon = 2;
                } else {
                    icon = 3;
                }
                return icon;
            },
            // 消息提示
            msg: function(content, type) {
                if(type != undefined) {
                    layer.msg(content, {icon: $.modal.icon(type), time: 1000, shift: 5});
                } else {
                    layer.msg(content);
                }
            },
            // 错误消息
            msgError: function(content) {
                $.modal.msg(content, modal_status.FAIL);
            },
            // 成功消息
            msgSuccess: function(content) {
                $.modal.msg(content, modal_status.SUCCESS);
            },
            // 警告消息
            msgWarning: function(content) {
                $.modal.msg(content, modal_status.WARNING);
            },
            // 消息提示并刷新父窗体
            msgReload: function(msg, type) {
                layer.msg(msg, {icon: $.modal.icon(type), time: 500, shade: [0.1, '#8F8F8F']}, function () {
                    $.modal.reload();
                });
            },
            // 弹出提示
            alert: function(content, type) {
                layer.alert(content, {
                    icon: $.modal.icon(type),
                    title: "系统提示",
                    btn: ['确认'],
                    btnclass: ['btn btn-primary']
                });
            },
            // 错误提示
            alertError: function(content) {
                $.modal.alert(content, modal_status.FAIL);
            },
            // 成功提示
            alertSuccess: function(content) {
                $.modal.alert(content, modal_status.SUCCESS);
            },
            // 警告提示
            alertWarning: function(content) {
                $.modal.alert(content, modal_status.WARNING);
            },
            // 确认窗体
            confirm: function(content, callback) {
                layer.confirm(content, {icon: 3, title: "系统提示", btn: ['确认', '取消']}, function (index) {
                    layer.close(index);
                    callback(true);
                });
            },
            // 打开遮罩层
            loading: function(message) {
                $.blockUI({message: '<div>' + message + '</div>'});
            },
            // 关闭遮罩层
            closeLoading: function() {
                setTimeout(function () {
                    $.unblockUI();
                }, 50);
            },
            // 关闭窗体
            close: function(index) {
                if($.common.isEmpty(index)) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                } else {
                    layer.close(index);
                }
            },
            // 关闭全部窗体
            closeAll: function() {
                layer.closeAll();
            },
            // 重新加载
            reload: function () {
                parent.location.reload();
            },
            // 弹出层指定宽度
            open: function (title, url, width, height) {
                if ($.common.isEmpty(width)) {
                    width = 800;
                }
                if ($.common.isEmpty(height)) {
                    height = ($(window).height() - 60);
                }
                layer.open({
                    type: 2,
                    area: [width + 'px', height + 'px'],
                    fix: false,
                    shade: 0.3,
                    title: title,
                    content: url,
                    btn: ['关闭'],
                    // 弹层外区域关闭
                    shadeClose: true,
                    cancel: function(index) {
                        return true;
                    }
                });
            }
        },
        // 操作封装处理
        operate: {
            // 提交数据
            submit: function(url, type, dataType, data, tableId, refresh, callback) {
                var config = {
                    url: url,
                    type: type,
                    dataType: dataType,
                    data: data,
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                    },
                    success: function(result) {
                        if(typeof callback == "function") {
                            callback(result);
                        }
                        $.operate.ajaxSuccess(result, tableId, refresh);
                    }
                };
                $.ajax(config);
            },
            // 保存结果，弹出msg，刷新table
            ajaxSuccess: function(result, tableId, refresh) {
                if(result.code == web_status.SUCCESS && refresh) {
                    $.modal.msgSuccess(result.msg);
                    $("#" + tableId).bootstrapTable('refresh', {silent: true});
                } else if(result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess(result.msg);
                } else if(result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg);
                } else {
                    $.modal.alertError(reuslt.msg)
                }
                $.modal.closeLoading();
            },
            // 保存成功，提示msg，父窗体全局更新
            saveSuccess: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.msgReload("保存成功，正在刷新数据请稍后......", modal_status.SUCCESS);
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                }  else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            }
        },
        // 通用方法封装处理
        common: {
            // 判断字符串是否为空
            isEmpty: function(value) {
                if(value == null || this.trim(value) == "") {
                    return true;
                }
                return false;
            },
            // 判断字符串是否非空
            isNotEmpty: function(value) {
                return !$.common.isEmpty(value);
            },
            // 数字正则表达式，只能为0-9数字
            numValid: function(text) {
                var patten = new RegExp(/^[0-9]+$/);
                return patten.test(text);
            },
            // 英文正则表达式，只能为a-z和A-Z字母
            enValid: function(text) {
                var patten = new RegExp(/^[a-zA-Z]+$/);
                return patten.test(text);
            },
            // 获取form下所有的字段并转换为json对象
            formToJSON: function(formId) {
                var json = {};
                $.each($("#" + formId).serializeArray(), function(i, field) {
                    if(json[field.name]) {
                        json[field.name] += ("," + field.value);
                    } else {
                        json[field.name] = field.value;
                    }
                });
                return json;
            }
        }
    });
})(jQuery);

/** 消息状态码 */
web_status = {
    SUCCESS: 0,
    FAIL: 500,
    WARNING: 301
};

/** 弹窗状态码 */
modal_status = {
    SUCCESS: "success",
    FAIL: "error",
    WARNING: "warning"
};